package wiki.hitime.stl.daq.web.controller.admin;

import wiki.hitime.stl.daq.bean.base.RespDTO;
import wiki.hitime.stl.daq.bean.dto.req.*;
import wiki.hitime.stl.daq.bean.dto.req.CheckServiceConfigListReqDTO;
import wiki.hitime.stl.daq.bean.dto.req.MsgRuleDetailConfigListReqDTO;
import wiki.hitime.stl.daq.service.admin.ConfigManageService;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author zhangdaoping
 * @create 2019-07-24 10:57
 */
@RestController
@RequestMapping("/admin/config")
public class ConfigManageController {

    private ConfigManageService configManageService;

    public ConfigManageController(ConfigManageService configManageService) {
        this.configManageService = configManageService;
    }

    /***
     * 获取数据源配置集合
     * @return
     */
    @GetMapping(value = "/dbType/list")
    public RespDTO listDbType() {
        return RespDTO.success(configManageService.listDbTypeConfig());
    }

    /**
     * 获取消息类型集合
     *
     * @return
     */
    @GetMapping(value = "/msgType/list")
    public RespDTO listMsgType() {
        return RespDTO.success(configManageService.listMsgType());
    }

    /**
     * 获取采集类型集合
     *
     * @return
     */
    @GetMapping(value = "/acquisitionType/list")
    public RespDTO listAcquisitionType() {
        return RespDTO.success(configManageService.listAcquisitionType());
    }

    /**
     * 获取值类型集合
     *
     * @return
     */
    @GetMapping(value = "/dataValueType/list")
    public RespDTO listDataValue() {
        return RespDTO.success(configManageService.listDataValueType());
    }

    /**
     * 获取检查类配置集合
     *
     * @param reqDTO
     * @param bindingResult
     * @return
     */
    @PostMapping(value = "/checkConfig/list")
    public RespDTO listCheckConfig(@Valid @RequestBody CheckServiceConfigListReqDTO reqDTO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return RespDTO.fail(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        return RespDTO.success(configManageService.listCheckServiceConfig(reqDTO));
    }

    /**
     * 获取消息详细配置集合
     *
     * @param reqDTO
     * @param bindingResult
     * @return
     */
    @PostMapping(value = "/msgRuleDetailConfig/list")
    public RespDTO listMsgRuleDetailConfig(@Valid @RequestBody MsgRuleDetailConfigListReqDTO reqDTO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return RespDTO.fail(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        return RespDTO.success(configManageService.listMsgDetailConfig(reqDTO));
    }

    /**
     * 获取消息参数集合
     *
     * @return
     */
    @GetMapping(value = "/msgParam/list")
    public RespDTO listMsgParam() {
        return RespDTO.success(configManageService.listMsgParamKey());
    }

    /**
     * 添加数据库类型配置
     *
     * @return
     */
    @PostMapping(value = "/dbType/add")
    public RespDTO addDbType(@Valid @RequestBody DbTypeConfigAddReqDTO reqDTO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return RespDTO.fail(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        configManageService.addDbTypeConfig(reqDTO);
        return RespDTO.success();
    }

    /**
     * 添加数据库详细配置
     *
     * @return
     */
    @PostMapping(value = "/dbDetail/add")
    public RespDTO addDbDetail(@Valid @RequestBody DbDetailConfigAddReqDTO reqDTO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return RespDTO.fail(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        configManageService.addDbDetailConfig(reqDTO);
        return RespDTO.success();
    }

    /**
     * 添加消息基础配置
     *
     * @return
     */
    @PostMapping(value = "/msgBase/add")
    public RespDTO addMsgBase(@Valid @RequestBody MsgBaseConfigAddReqDTO reqDTO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return RespDTO.fail(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        configManageService.addMsgBaseConfig(reqDTO);
        return RespDTO.success();
    }
}
